package com.example.mybatisplus.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.UserService;
import com.example.mybatisplus.model.domain.User;

import javax.xml.bind.util.JAXBSource;

import java.util.List;

import static com.example.mybatisplus.common.Constants.ROLE_CONSULTANT;
import static com.example.mybatisplus.common.Constants.ROLE_FIRSTVISIT;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2022-05-29
 * @version v1.0
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger( UserController.class );

    @Autowired
    private UserService userService;

    @GetMapping("")
    public JsonResponse show(){
        return JsonResponse.success(userService.list(),"查询成功");
    }

    @GetMapping("/role/firstvisit")
    public JsonResponse getFirstvisit(){
        return JsonResponse.success(userService.getRole(ROLE_FIRSTVISIT)).setMessage("查询初访师成功");
    }

    @GetMapping("/spareconsultants/{id}")
    public JsonResponse getSpareConsultantsByTimeId(@PathVariable("id") Long id){
        System.out.println(id);
        return JsonResponse.success(userService.getSpareConsultantsByTimeId(id)).setMessage("查询空闲咨询师成功");
    }

    @GetMapping("/sparefirstvisitmen/{id}")
    public JsonResponse getSpareFirstvisitmenByTimeId(@PathVariable("id") Long id){
        System.out.println(id);
        return JsonResponse.success(userService.getSpareFirstvisitsByTimeId(id)).setMessage("查询空闲初访师成功");
    }

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        User  user = (User) userService.getById(id);
        return JsonResponse.success(user);
    }

    @GetMapping("/consultant/{id}")
    public JsonResponse getConsultantById(@PathVariable("id") Long id) throws Exception {
        return JsonResponse.success(userService.getConsultantById(id),"按id查询咨询师成功");
    }

    @GetMapping("/firstvisits/{id}")
    public JsonResponse getFirstvisitsById(@PathVariable("id") Long id) {
        return JsonResponse.success(userService.getByIdAndType(id,ROLE_FIRSTVISIT)).setMessage("按id查询初访师成功");
    }

    @GetMapping("/consultants")
    public JsonResponse getConsultants(){
        return JsonResponse.success(userService.getConsultants(),"查询咨询师成功");
    }

    @GetMapping("/workers")
    public JsonResponse getWorkers(){
        List<User> users = userService.getRole(ROLE_FIRSTVISIT);
        users.addAll(userService.getRole(ROLE_CONSULTANT));
        return JsonResponse.success(users).setMessage("查询工作人员成功");
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        userService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateUser(User  user) throws Exception {
        userService.updateById(user);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建User
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(User  user) throws Exception {
        userService.save(user);
        return JsonResponse.success(null);
    }
}

